package com.zzyl.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@Api(tags = "用户管理")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户添加
     *
     * @param userDto
     * @return
     */
    @PutMapping
    @ApiOperation("用户添加")
    @ApiImplicitParam(name = "userDto", value = "用户DTO对象", required = true, dataType = "UserDto")
    @ApiOperationSupport(includeParameters = {
            "userDto.dataState",
            "userDto.realName",
            "userDto.roleVoIds",
            "userDto.deptNo",
            "userDto.postNo",
            "userDto.email",
            "userDto.mobile"})
    public ResponseResult<UserVo> createUser(@RequestBody UserDto userDto) throws CloneNotSupportedException {
        Boolean flag = userService.createUser(userDto);
        return ResponseResult.success(flag);
    }

    /**
     * 用户分页
     *
     * @param userDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userDto", value = "用户DTO对象", required = true, dataType = "UserDto"),
            @ApiImplicitParam(paramType = "path", name = "pageNum", value = "页码", example = "1", dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "pageSize", value = "每页条数", example = "10", dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {
            "userDto.dataState",
            "userDto.deptNo",
            "userDto.email",
            "userDto.postNo",
            "userDto.realName",
            "userDto.roleId"})
    ResponseResult<PageResponse<UserVo>> findUserPage(
            @RequestBody UserDto userDto,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        PageResponse<UserVo> list = userService.list(userDto, pageNum, pageSize);
        return ResponseResult.success(list);
    }

    /**
     * 用户修改
     */
    @PatchMapping
    @ApiOperation(value = "用户修改", notes = "用户修改")
    @ApiImplicitParam(name = "userDto", value = "用户DTO对象", required = true, dataType = "UserDto")
    @ApiOperationSupport(includeParameters = {
            "userDto.realName",
            "userDto.roleVoIds",
            "userDto.deptNo",
            "userDto.postNo",
            "userDto.dataState",
            "userDto.email",
            "userDto.mobile"
    })
    public ResponseResult<Boolean> updateUserInfo(@RequestBody UserDto userDto) throws CloneNotSupportedException {
        Boolean flag = userService.update(userDto);
        return ResponseResult.success(flag);
    }

    /**
     * 启用禁用
     */
    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation("启用禁用")
    public ResponseResult<Boolean> updateStatus(@PathVariable("id") Long id,@PathVariable("status") String status){
        Boolean flag = userService.updateStatus(id,status);
        return ResponseResult.success(flag);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/remove/{userIds}")
    @ApiOperation("删除用户")
    public ResponseResult<Boolean> delete(@PathVariable Long userIds){
        Boolean flag = userService.delete(userIds);
        return ResponseResult.success(flag);
    }

    /**
     * 密码重置
     */
    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation("密码重置")
    public ResponseResult<Boolean> passwordReset(@PathVariable Long userId){
        Boolean flag = userService.updatePassword(userId);
        return ResponseResult.success(flag);
    }

    /**
     * 用户列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "用户列表", notes = "用户列表")
    @ApiImplicitParam(name = "userDto", value = "用户DTO对象", required = true, dataType = "UserDto")
    @ApiOperationSupport(includeParameters = "userDto.realName")
    public ResponseResult<List<UserVo>> userList(@RequestBody UserDto userDto) throws CloneNotSupportedException {
        List<UserVo> list = userService.findList(userDto);
        return ResponseResult.success(list);
    }
}
